package com.example.team.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;
import java.io.IOException;

@Controller
public class DownFileController {
    @GetMapping("/downFile")
    public String downFile(){
        System.out.println("进入访问下载模版界面");
        return "downfile";
    }
    @GetMapping("/dwon")
    public ResponseEntity<byte[]>FileDown(String filename){
        System.out.println("进入下载文件方法，下载文件名为："+filename);
        String dirPath="D:/file/";
        File file=new File(dirPath+File.separator+filename);
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",filename);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try{
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
        }catch (IOException e){
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
